package com.tjsinfo.tjsoa.workReport.bo;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;

import com.tjsinfo.tjsoa.Dao.IHibernateDao;
import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.workReport.vo.TResourceWorkReport;

public class TResourceWorkReportManageImpl implements TResourceWorkReportManage {
	private IHibernateDao hibernateDao;
	private IQbcDao qbcDao;
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	public IHibernateDao getHibernateDao() {
		return hibernateDao;
	}

	public void setHibernateDao(IHibernateDao hibernateDao) {
		this.hibernateDao = hibernateDao;
	}
	
	public void delete(Integer id){
		TResourceWorkReport resourceWorkReport =	(TResourceWorkReport) hibernateDao.findById(TResourceWorkReport.class, id);
		hibernateDao.delete(resourceWorkReport);
	}
	public void delete(String propertyName, Object value) {
		List<TResourceWorkReport>  reports = findByProperty(propertyName, value);
		for (TResourceWorkReport tResourceWorkReport : reports) {
			hibernateDao.delete(tResourceWorkReport);
		}
		
	}

	public TResourceWorkReport findById(Integer id) {
		return (TResourceWorkReport) hibernateDao.findById(TResourceWorkReport.class, id);
	}

	public List findByProperty(String propertyName, Object value) {
		return hibernateDao.findByProperty(TResourceWorkReport.class, propertyName, value);
	}

	public int save(TResourceWorkReport transientInstance) {
		return  hibernateDao.save(transientInstance);
		
	}

	public boolean update(TResourceWorkReport transientInstance) {
		// TODO Auto-generated method stub
		try {
			hibernateDao.update(transientInstance);
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}

	public PaginationSupport findOrderList(Integer userId,Integer page,Integer pageSize) {
		// TODO Auto-generated method stub
		List<Order> orderList=new ArrayList<Order>();
		orderList.add(Order.desc("orderNo"));
		orderList.add(Order.desc("id"));
		int startIndex=pageSize*page;
		DetachedCriteria criteria = DetachedCriteria.forClass(TResourceWorkReport.class);
		SimpleExpression approveMeListUserId = Restrictions.eq("userId", userId);
		criteria.add(approveMeListUserId);
		PaginationSupport paginationSupport=qbcDao.findPageByCriteria(criteria,orderList, Integer.valueOf(pageSize),startIndex);

		return paginationSupport;
	}
	
	public PaginationSupport findOrderList(Integer page,Integer pageSize) {
		// TODO Auto-generated method stub
		List<Order> orderList=new ArrayList<Order>();
		orderList.add(Order.desc("orderNo"));
		orderList.add(Order.desc("id"));
		int startIndex=pageSize*page;
		DetachedCriteria criteria = DetachedCriteria.forClass(TResourceWorkReport.class);
		PaginationSupport paginationSupport=qbcDao.findPageByCriteria(criteria,orderList, Integer.valueOf(pageSize),startIndex);

		return paginationSupport;
	}

}
